var util=require('util');
var database=require('../config/database.json');
var host=database.host;
var db=database.db;
var mongodb=require('../DAOImpl/mongodb.js');
var mon1=new mongodb(host,db);
var status=require('../config/status.json');
var google = require('../private/googleService/service');

function sleep(milliSecond) {  
      
    var startTime = new Date().getTime();  
      
    console.log(startTime);  
      
    while(new Date().getTime() <= milliSecond + startTime) {  
          
    }      
    console.log(new Date().getTime());  
}  

function bookNow(req,res,next){ 
	var result=new Object(); 
    var query2={ 
    		"latitude":{ $gt:parseInt(req.body.latitude)-50.0,$lt:parseInt(req.body.latitude)+50.0},
    		"longitude":{ $gt:parseInt(req.body.longitude)-50.0,$lt:parseInt(req.body.longitude)+50.0}
    		};
    mon1.find(database.col_driver,query2,function(err1,err2,doc){
    	if(err1||err2){
			result.status=status.SHOWORDER.FAIL
		}else{
			if(doc){
		    	result.status=status.BOOKNOW.SUCCESS,
				result.driverInfo=doc,
				result.distance="123",
				result.time="123"
				}
				else{
					result.status=status.BOOKNOW.NO_MATCH,
					result.driverInfo="",
					result.distance="",
					result.time=""
					}
		}

		//----------GET DISTANCE
		/*var googleQuery = new Object();
		var googleResult=new Object();
		googleQuery.origin = query.latitude+ "," + query.longitude;
		googleQuery.destination = doc.latitude+"," +doc,longitude;
		google.getDistance(googleQuery, function(err, data){
			if (!err) {
				googleresult = {
					'resultCode' : 0,
					'distance' : data.routes[0].legs[0].distance,
					'duration' : data.routes[0].legs[0].duration
				};
			} else {
				var googleresult = {
					'resultCode' : -1
				};
			}
		});*/
    	 console.log(result);
         res.send(result);
            
    });

    return next();
}
/*
 * @req.body: cus_id
 * {'cus_id': '1'}
 */

function showOrder(req, res, next){
	var result=new Object();
	var query={
		"cusId":req.body.cusId
	};
	mon1.findOne(database.col_order,query,function(err1,err2,doc){
			var query2={
					"driverId":doc.driverId
			};
			mon1.findOne(database.col_driver,query2,function(err3,err4,doc1){
				if(err1||err2||err3||err4){
					result.status=status.SHOWORDER.FAIL
				}else{
					result.status=status.SHOWORDER.SUCCESS
				}
				result.dri=new Object();
				result.dri_info.id=doc1.id;
				result.dri_info.name=doc1.name;
				result.dri_info.taxi_number=doc1.taxi_number;
				result.dri_info.phone=doc1.phone;
				result.dri_info.credit=doc1.credit;
				result.dri_info.photo=doc1.photo;
				
				result.order.order_id=doc.order_id;
				result.order.time=doc.time;
				result.order.start=doc.start;
				result.order.order_id=doc.end;
				
				result.succ_num=doc.length;
				result.waiting_num='1';
				
				res.send(result);	
				console.log(result);
			});
	});

	return next();
}

/*
 * @req.body: cus_id
 * {'cus_id': '1'}
 */

function showHisOrder(req, res, next){
	var result=new Object();
	var query={
			"cus_Id":req.body.cus_id
		};
		mon1.find(database.col_order,query,function(err1,err2,doc){
			
				//var query=new Object();
			
				for(var i=0;i<=doc.length;i++){
				//	query.i.id=doc[i].dri_Id;
					var id1=doc[i].dri_Id;
					var query={
							"id":id1
					};
					
					 console.log(doc[i].dri_Id);
					//console.log(doc);
					 mon1.find(database.col_driver,query,function(err3,err4,doc1){
							if(err1||err2||err3||err4){
								result.status=status.SHOWORDER.FAIL
							}else{
								result.status=status.SHOWORDER.SUCCESS
							}
							result.dri_info=new Object();
							result.dri_info.id=doc1.id;
							result.dri_info.name=doc1.name;
							result.dri_info.taxi_number=doc1.taxi_number;
							result.dri_info.phone=doc1.phone;
							result.dri_info.credit=doc1.credit;
							result.dri_info.photo=doc1.photo;
							
							result.order.order_id=doc.order_id;
							result.order.time=doc.time;
							result.order.start=doc.start;
							result.order.order_id=doc.end;
							
							result.succ_num=doc.length;
							result.waiting_num='1';
							console.log(doc);
							//console.log(result);
							res.send(result);	
						});
				};
			
		});

		return next();
}

/*
 * @req.body: cus_id
 * {'order_id': '1'}
 */

function cancelOrder(req, res, next){
	var result=new Object();
	var selector={
		"id":req.body.order_id	
	};
	mon1.remove(database.col_order,selector,function(err1,err2,num){
		if(err1||err2){
			result.status=status.SHOWORDER.FAIL
		}else{
			if(num>0){
				console.log("cancel sucess");
				result.status=status.SHOWORDER.SUCCESS;
			}else{
				console.log("cancel not sucess");
			}
		}
	});
	
	res.send(result);
	return next;
}


exports.services = [ bookNow, showOrder, showHisOrder, cancelOrder ];